package com.client.listeners;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.client.GUIMonitor;
import com.client.answers.Answer;
import com.client.answers.CreateRoomAnswer;
import com.client.answers.SimpleAnswer;

public class CreateButtonListener implements ActionListener {
	private JPasswordField		passField;
	private JTextField			textField;
	private JFrame				frame;
	@SuppressWarnings("unused")
	private JList				roomList;
	private DefaultListModel	listModel;
	private GUIMonitor			guim;

	public CreateButtonListener(JList roomList, DefaultListModel listModel, GUIMonitor guim)
	{
		this.guim = guim;
		this.roomList = roomList;
		this.listModel = listModel;
		passField = new JPasswordField(10);
		passField.setActionCommand("OK");
		passField.addActionListener(this);
		textField = new JTextField();
		textField.setColumns(10);
		textField.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		guim.setAnswer(GUIMonitor.JOIN_OR_CREATE_ACTION, new SimpleAnswer(Answer.CREATE));
		// Create frame and components
		frame = new JFrame("Create room");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		JLabel nameLabel = new JLabel("Room name: ");
		JLabel passLabel = new JLabel("Password:     ");

		// Put components in panels
		JPanel namePanel = new JPanel();
		namePanel.add(nameLabel);
		namePanel.add(textField);
		JPanel passPanel = new JPanel();
		passPanel.add(passLabel);
		passPanel.add(passField);

		// Add panels to frame and set frame settings
		frame.setPreferredSize(new Dimension(250, 100));
		frame.getContentPane().add(namePanel, BorderLayout.NORTH);
		frame.getContentPane().add(passPanel, BorderLayout.CENTER);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.pack();
		frame.setVisible(true);

		// System.out.println("CREATE CLICKED");
		String cmd = e.getActionCommand();
		if (cmd.equals("OK"))
		{
			// Process the password.
			char[] passInput = passField.getPassword();
			String roomPass = new String(passInput);
			String roomName = textField.getText();

			// SKAPA ETT RUM H�R! VAR SKA GUIT SKAPAS OCH F� TILLG�NG TILL ROOMHANDLER?!
			// rh.createRoom(roomName, Util.SHA1(roomPass));
			guim.setAnswer(GUIMonitor.ROOM_CREATE_ACTION, new CreateRoomAnswer(roomName, roomPass));
			listModel.addElement(roomName);
			// roomList.setSelectedIndex(4);
			// roomList.ensureIndexIsVisible(4);

			Arrays.fill(passInput, '0');
			// passField = new JPasswordField(10);
			// textField = new JFormattedTextField();
			passField.setText("");
			textField.setText("");

			// Framen st�ngs inte av om man har den i en if-sats....
			frame.setVisible(false);
			frame.dispose();
		}
	}
}
